﻿using System.Collections.Generic;
using System.Linq;
using ClassRoom.Models;
using MongoDB.Bson;
using MongoDB.Driver;

namespace ClassRoom
{
    public class DbAccess
    {
        public void WriteLog(LogModel logModel)
        {
            var collection = MvcApplication.MongoDatabase.GetCollection<BsonDocument>("log");
            var result = collection.Insert(logModel);
        }

        public BsonDocument FindOne(IMongoQuery filter)
        {
            var collection = MvcApplication.MongoDatabase.GetCollection<BsonDocument>("user");
            var result = collection.FindOne(filter);
            return result;
        }

        public List<BsonDocument> Find(IMongoQuery filter)
        {
            var collection = MvcApplication.MongoDatabase.GetCollection<BsonDocument>("user");
            var sourceCursor = collection.Find(filter);
            return sourceCursor.ToList();
        }

        public BsonValue GetPosts(string userId)
        {
            BsonValue bv = MvcApplication.MongoDatabase.Eval("getPost");
            BsonValue bv1 = MvcApplication.MongoDatabase.Eval(bv.AsBsonJavaScript.Code, userId);
            return bv1;
        }

        public BsonValue GetProjects(string userId)
        {
            BsonValue bv = MvcApplication.MongoDatabase.Eval("getProjects");
            BsonValue bv1 = MvcApplication.MongoDatabase.Eval(bv.AsBsonJavaScript.Code, userId);
            return bv1;
        }

        public void CreateNewPost(string fromUserId, string toUserId, string content)
        {
            BsonValue bv = MvcApplication.MongoDatabase.Eval("createNewPost");
            BsonValue bv1 = MvcApplication.MongoDatabase.Eval(bv.AsBsonJavaScript.Code, fromUserId, toUserId, content);
        }

        public void PushCommentToPost(string fromUser, MsgModel msgMode)
        {
            BsonValue bv = MvcApplication.MongoDatabase.Eval("pushComment");
            BsonValue bv1 = MvcApplication.MongoDatabase.Eval(bv.AsBsonJavaScript.Code, fromUser, msgMode.msgId, msgMode.content);
        }

        public void SaveProject(string userId, string id, string name, string desc, string file, string student, string supervisor, string secondMarker)
        {
            BsonValue bv = MvcApplication.MongoDatabase.Eval("saveProject");
            BsonValue bv1 = MvcApplication.MongoDatabase.Eval(bv.AsBsonJavaScript.Code, userId, id, name, desc, file, student, supervisor, secondMarker);
        }

        public BsonValue GetStatistic(string userId)
        {
            BsonValue bv = MvcApplication.MongoDatabase.Eval("getStatistic");
            BsonValue bv1 = MvcApplication.MongoDatabase.Eval(bv.AsBsonJavaScript.Code, userId);
            return bv1;
        }
    }
}